package com.tancire.report.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.enums.BusinessType;
import com.tancire.report.domain.ReportAppFlow;
import com.tancire.report.service.IReportAppFlowService;
import com.tancire.common.utils.poi.ExcelUtil;
import com.tancire.common.core.page.TableDataInfo;

/**
 * 应用接口流量Controller
 *
 * @author tancire
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/report/reportAppFlow")
public class ReportAppFlowController extends BaseController {
    @Resource
    private IReportAppFlowService reportAppFlowService;

    /**
     * 查询应用接口流量列表
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportAppFlow reportAppFlow) {
        startPage();
        List<ReportAppFlow> list = reportAppFlowService.selectReportAppFlowList(reportAppFlow);
        return getDataTable(list);
    }

    /**
     * 导出应用接口流量列表
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:export')")
    @Log(title = "应用接口流量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportAppFlow reportAppFlow) {
        List<ReportAppFlow> list = reportAppFlowService.selectReportAppFlowList(reportAppFlow);
        ExcelUtil<ReportAppFlow> util = new ExcelUtil<ReportAppFlow>(ReportAppFlow.class);
        util.exportExcel(response, list, "应用接口流量数据");
    }

    /**
     * 获取应用接口流量详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(reportAppFlowService.selectReportAppFlowById(id));
    }

    /**
     * 新增应用接口流量
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:add')")
    @Log(title = "应用接口流量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportAppFlow reportAppFlow) {
        return toAjax(reportAppFlowService.insertReportAppFlow(reportAppFlow));
    }

    /**
     * 修改应用接口流量
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:edit')")
    @Log(title = "应用接口流量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportAppFlow reportAppFlow) {
        return toAjax(reportAppFlowService.updateReportAppFlow(reportAppFlow));
    }

    /**
     * 删除应用接口流量
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:remove')")
    @Log(title = "应用接口流量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(reportAppFlowService.deleteReportAppFlowByIds(ids));
    }

    /**
     * 查询应用接口流量列表
     */
    @PreAuthorize("@ss.hasPermi('report:reportAppFlow:report')")
    @GetMapping("/all/list")
    public AjaxResult listReport(ReportAppFlow reportAppFlow) {
        // 日期区间为空默认查7天内的
        if (StringUtils.isBlank(reportAppFlow.getBeginDay())) {
            String beginDay = DateUtils.getYdMoreDay(7);
            reportAppFlow.setBeginDay(beginDay);
        }
        if (StringUtils.isBlank(reportAppFlow.getEndDay())) {
            // 结束日期
            String endDay = DateUtils.getTdDay();
            reportAppFlow.setEndDay(endDay);
        }
        List<ReportAppFlow> list = reportAppFlowService.selectReportAppFlowList(reportAppFlow);
        // 日期处理
        List<String> dayList =  DateUtils.getDayAreaList(reportAppFlow.getBeginDay(), reportAppFlow.getEndDay());
        // 转换成map
        Map<String, List<ReportAppFlow>> map = new TreeMap<>();
        for (String day : dayList) {
            List<ReportAppFlow> collect = list.stream().filter(x -> x.getReportDate().equals(day)).collect(Collectors.toList());
            map.put(day, collect);
        }
        return AjaxResult.success("查询成功", map);
    }
}
